import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/campaign_controller.dart';
import 'package:efood_multivendor/controller/cart_controller.dart';
import 'package:efood_multivendor/controller/product_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/controller/user_controller.dart';
import 'package:efood_multivendor/controller/wishlist_controller.dart';
import 'package:efood_multivendor/data/model/response/cart_model.dart';
import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/helper/date_converter.dart';
import 'package:efood_multivendor/helper/price_converter.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/age_dialog.dart';
import 'package:efood_multivendor/view/base/custom_button.dart';
import 'package:efood_multivendor/view/base/custom_image.dart';
import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:efood_multivendor/view/base/discount_tag.dart';
import 'package:efood_multivendor/view/base/quantity_button.dart';
import 'package:efood_multivendor/view/base/rating_bar.dart';
import 'package:efood_multivendor/view/screens/checkout/checkout_screen.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ProductBottomSheet extends StatefulWidget {
  final Product product;
  final bool isCampaign;
  final CartModel? cart;
  final int? cartIndex;
  final bool inRestaurantPage;
  ProductBottomSheet(
      {required this.product,
      this.isCampaign = false,
      this.cart,
      this.cartIndex,
      this.inRestaurantPage = false});

  @override
  State<ProductBottomSheet> createState() => _ProductBottomSheetState();
}

class _ProductBottomSheetState extends State<ProductBottomSheet>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  CampaignController campaignControllerInit = Get.find<CampaignController>();
  @override
  void initState() {
    super.initState();
    Get.find<ProductController>().initData(widget.product, widget.cart);
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 1),
    );
    _animation = Tween<double>(
      begin: 0,
      end: 1,
    ).animate(_controller);
    _controller.repeat(reverse: true);
    if ((widget.product.addOns?.length ?? 0) >= 1) {
      campaignControllerInit.addOnQtyList.clear();
      campaignControllerInit.addonList.clear();
      campaignControllerInit.addOnPriceList.clear();
      widget.product.addOns!.forEach((addOn) {
        campaignControllerInit.addOnQtyList.add(0);
        campaignControllerInit.addOnPriceList.add(0);
      });
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  static const int YearInHour = 8760;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 550,
      padding: EdgeInsets.only(
          left: Dimensions.PADDING_SIZE_DEFAULT,
          bottom: Dimensions.PADDING_SIZE_DEFAULT),
      decoration: BoxDecoration(
        color: Theme.of(context).cardColor,
        borderRadius: ResponsiveHelper.isMobile(context)
            ? BorderRadius.vertical(
                top: Radius.circular(Dimensions.RADIUS_EXTRA_LARGE))
            : BorderRadius.all(Radius.circular(Dimensions.RADIUS_EXTRA_LARGE)),
      ),
      child: GetBuilder<ProductController>(builder: (productController) {
        return GetBuilder<CampaignController>(builder: (campaignController) {
          double? _startingPrice;
          double? _endingPrice;
          List<AddOn> _addOnIdList = [];
          double addonsCost = 0;
          List<String> _variationList = [];
          String variationType = '';
          bool isFirst = true;
          double? price = widget.product.price;
          Variation? _variation;

          if (widget.product.choiceOptions?.isNotEmpty == true) {
            List<double> _priceList = [];
            widget.product.variations
                ?.forEach((variation) => _priceList.add(variation.price!));
            _priceList.sort((a, b) => a.compareTo(b));
            _startingPrice = _priceList[0];
            if (_priceList[0] < _priceList[_priceList.length - 1]) {
              _endingPrice = _priceList[_priceList.length - 1];
            }
          } else {
            _startingPrice = widget.product.price;
          }

          for (int index = 0;
              index < widget.product.choiceOptions!.length;
              index++) {
            _variationList.add(widget.product.choiceOptions![index]
                .options![productController.variationIndex![index]]
                .replaceAll(' ', ''));
          }

          _variationList.forEach((variation) {
            if (isFirst) {
              variationType = '$variationType$variation';
              isFirst = false;
            } else {
              variationType = '$variationType-$variation';
            }
          });

          for (Variation variation in widget.product.variations!) {
            if (variation.type == variationType) {
              price = variation.price;
              _variation = variation;
              break;
            }
          }
          if (widget.product.addOns!.length >= 1) {
            for (int index = 0;
                index < campaignController.addonList.length;
                index++) {
              if (widget.product.addOns!
                  .contains(campaignController.addonList[index])) {
                int i = widget.product.addOns!
                    .indexOf(campaignController.addonList[index]);
                AddOn newAddOn = AddOn(
                    id: campaignController.addonList[index].id,
                    quantity: campaignController.addOnQtyList[i],
                    price: campaignController.addOnPriceList[i]);

                addonsCost = addonsCost +
                    campaignController.addOnQtyList[i] *
                        campaignController.addOnPriceList[i];

                _addOnIdList.add(newAddOn);
              }
            }
          }

          double? _discount =
              (widget.isCampaign || widget.product.restaurantDiscount == 0)
                  ? widget.product.discount
                  : widget.product.restaurantDiscount;
          String? _discountType =
              (widget.isCampaign || widget.product.restaurantDiscount == 0)
                  ? widget.product.discountType
                  : 'percent';
          double priceWithDiscount = PriceConverter.convertWithDiscount(
              price!, _discount!, _discountType!);
          double priceWithQuantity =
              priceWithDiscount * productController.quantity;

          double priceWithAddons = priceWithQuantity + addonsCost;
          bool? _isRestAvailable = widget.product.is_open;

          bool? _isFoodAvailable = widget.product.is_open;

          bool? _isAvailable =
              _isRestAvailable == true && _isFoodAvailable == true;

          CartModel _cartModel = CartModel(
              widget.product.price,
              priceWithDiscount,
              _variation != null ? [_variation] : [],
              (price -
                  PriceConverter.convertWithDiscount(
                      price, _discount, _discountType)),
              productController.quantity,
              _addOnIdList,
              campaignControllerInit.addonList,
              widget.isCampaign,
              widget.product,
              0);
          return WillPopScope(
            onWillPop: () async {
              if (campaignControllerInit.addOnQtyList.isNotEmpty) {
                campaignControllerInit.addOnQtyList.clear();
              }
              return true;
            },
            child: SingleChildScrollView(
              child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    ResponsiveHelper.isDesktop(context)
                        ? InkWell(
                            onTap: () => Get.back(), child: Icon(Icons.close))
                        : SizedBox(),
                    Padding(
                      padding: EdgeInsets.only(
                        right: Dimensions.PADDING_SIZE_DEFAULT,
                        top: ResponsiveHelper.isDesktop(context)
                            ? 0
                            : Dimensions.PADDING_SIZE_DEFAULT,
                      ),
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Stack(children: [
                                    ClipRRect(
                                      borderRadius: BorderRadius.circular(
                                          Dimensions.RADIUS_SMALL),
                                      child: CustomImage(
                                        image: '${widget.product.image}',
                                        width:
                                            ResponsiveHelper.isMobile(context)
                                                ? 100
                                                : 140,
                                        height:
                                            ResponsiveHelper.isMobile(context)
                                                ? 100
                                                : 140,
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                    DiscountTag(
                                        discount: _discount,
                                        discountType: _discountType,
                                        fromTop: 20),
                                  ]),
                                  SizedBox(width: 10),
                                  Expanded(
                                    child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            widget.product.name ?? '',
                                            style: sfMedium.copyWith(
                                                fontSize:
                                                    Dimensions.fontSizeLarge),
                                            maxLines: 2,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                          InkWell(
                                            onTap: () {
                                              if (widget.inRestaurantPage) {
                                                Get.back();
                                              } else {
                                                Get.offNamed(RouteHelper
                                                    .getRestaurantRoute(widget
                                                        .product
                                                        .restaurantId!));
                                              }
                                            },
                                            child: Padding(
                                              padding: EdgeInsets.fromLTRB(
                                                  0, 5, 5, 5),
                                              child: Text(
                                                  widget.product
                                                          .restaurantName ??
                                                      '',
                                                  style: sfRegular.copyWith(
                                                      fontSize: Dimensions
                                                          .fontSizeSmall,
                                                      color: Theme.of(context)
                                                          .colorScheme
                                                          .secondary)),
                                            ),
                                          ),
                                          RatingBar(
                                              rating: widget.product.avgRating!,
                                              size: 15,
                                              ratingCount:
                                                  widget.product.ratingCount!),
                                          Text(
                                            '${PriceConverter.convertPrice(_startingPrice!, discount: _discount, discountType: _discountType)}'
                                            '${_endingPrice != null ? ' - ${PriceConverter.convertPrice(_endingPrice, discount: _discount, discountType: _discountType)}' : ''}',
                                            style: sfMedium.copyWith(
                                                fontSize:
                                                    Dimensions.fontSizeLarge),
                                          ),
                                          price > priceWithDiscount
                                              ? Text(
                                                  '${PriceConverter.convertPrice(_startingPrice)}'
                                                  '${_endingPrice != null ? ' - ${PriceConverter.convertPrice(_endingPrice)}' : ''}',
                                                  style: sfMedium.copyWith(
                                                      color: Theme.of(context)
                                                          .disabledColor,
                                                      decoration: TextDecoration
                                                          .lineThrough,
                                                      decorationColor:
                                                          Theme.of(context)
                                                              .disabledColor),
                                                )
                                              : SizedBox(),
                                        ]),
                                  ),
                                  Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Get.find<SplashController>()
                                                    .configModel
                                                    ?.toggleVegNonVeg ==
                                                true
                                            ? Container(
                                                padding: EdgeInsets.symmetric(
                                                    vertical: Dimensions
                                                        .PADDING_SIZE_EXTRA_SMALL,
                                                    horizontal: Dimensions
                                                        .PADDING_SIZE_SMALL),
                                                decoration: BoxDecoration(
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            Dimensions
                                                                .RADIUS_SMALL),
                                                    gradient: LinearGradient(
                                                      colors: [
                                                        Color(0xffff8022),
                                                        Color(0xffff2222)
                                                      ],
                                                    )),
                                                child: Text(
                                                  widget.product.veg == 0
                                                      ? 'non_fasting'.tr
                                                      : 'fasting'.tr,
                                                  style: sfRegular.copyWith(
                                                      fontSize: Dimensions
                                                          .fontSizeExtraSmall,
                                                      color: Colors.white),
                                                ),
                                              )
                                            : SizedBox(),
                                        SizedBox(
                                            height: Get.find<SplashController>()
                                                        .configModel
                                                        ?.toggleVegNonVeg ==
                                                    true
                                                ? 50
                                                : 0),
                                        widget.isCampaign
                                            ? SizedBox(height: 25)
                                            : GetBuilder<WishListController>(
                                                builder: (wishList) {
                                                return InkWell(
                                                  onTap: () {
                                                    if (Get.find<
                                                            AuthController>()
                                                        .isLoggedIn()) {
                                                      wishList.wishProductIdList
                                                              .contains(widget
                                                                  .product.id)
                                                          ? wishList
                                                              .removeFromWishList(
                                                                  widget.product
                                                                      .id!,
                                                                  false)
                                                          : wishList
                                                              .addToWishList(
                                                                  widget
                                                                      .product,
                                                                  null,
                                                                  false);
                                                    } else {
                                                      showCustomSnackBar(
                                                          'you_are_not_logged_in'
                                                              .tr);
                                                    }
                                                  },
                                                  child: Icon(
                                                    wishList.wishProductIdList
                                                            .contains(widget
                                                                .product.id)
                                                        ? Icons.favorite
                                                        : Icons.favorite_border,
                                                    color: wishList
                                                            .wishProductIdList
                                                            .contains(widget
                                                                .product.id)
                                                        ? Theme.of(context)
                                                            .primaryColor
                                                        : Theme.of(context)
                                                            .disabledColor,
                                                  ),
                                                );
                                              }),
                                      ]),
                                ]),
                            SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                            (widget.product.description != null &&
                                    widget.product.description?.isNotEmpty ==
                                        true)
                                ? Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text('description'.tr, style: sfMedium),
                                      SizedBox(
                                          height: Dimensions
                                              .PADDING_SIZE_EXTRA_SMALL),
                                      Text(widget.product.description ?? '',
                                          style: sfRegular),
                                      SizedBox(
                                          height:
                                              Dimensions.PADDING_SIZE_LARGE),
                                    ],
                                  )
                                : SizedBox(),
                            ListView.builder(
                              shrinkWrap: true,
                              itemCount: widget.product.choiceOptions?.length,
                              physics: NeverScrollableScrollPhysics(),
                              itemBuilder: (context, index) {
                                return Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                          widget.product.choiceOptions?[index]
                                                  .title ??
                                              '',
                                          style: sfMedium),
                                      SizedBox(
                                          height: Dimensions
                                              .PADDING_SIZE_EXTRA_SMALL),
                                      GridView.builder(
                                        gridDelegate:
                                            SliverGridDelegateWithFixedCrossAxisCount(
                                          crossAxisCount:
                                              ResponsiveHelper.isMobile(context)
                                                  ? 3
                                                  : 4,
                                          crossAxisSpacing: 20,
                                          mainAxisSpacing: 10,
                                          childAspectRatio: (1 / 0.25),
                                        ),
                                        shrinkWrap: true,
                                        physics: NeverScrollableScrollPhysics(),
                                        itemCount: widget
                                            .product
                                            .choiceOptions?[index]
                                            .options
                                            ?.length,
                                        itemBuilder: (context, i) {
                                          return InkWell(
                                            onTap: () {
                                              productController
                                                  .setCartVariationIndex(
                                                      index, i);
                                            },
                                            child: Container(
                                              alignment: Alignment.center,
                                              padding: EdgeInsets.symmetric(
                                                  horizontal: Dimensions
                                                      .PADDING_SIZE_EXTRA_SMALL),
                                              decoration: BoxDecoration(
                                                color: productController
                                                                .variationIndex?[
                                                            index] !=
                                                        i
                                                    ? Theme.of(context)
                                                        .colorScheme
                                                        .surface
                                                    : Theme.of(context)
                                                        .primaryColor,
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        Dimensions
                                                            .RADIUS_SMALL),
                                                border: productController
                                                                .variationIndex?[
                                                            index] !=
                                                        i
                                                    ? Border.all(
                                                        color: Theme.of(context)
                                                            .disabledColor,
                                                        width: 2)
                                                    : null,
                                              ),
                                              child: Text(
                                                widget
                                                        .product
                                                        .choiceOptions?[index]
                                                        .options?[i]
                                                        .trim() ??
                                                    '',
                                                maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                                style: sfRegular.copyWith(
                                                  color: productController
                                                                  .variationIndex?[
                                                              index] !=
                                                          i
                                                      ? Colors.black
                                                      : Colors.white,
                                                ),
                                              ),
                                            ),
                                          );
                                        },
                                      ),
                                      SizedBox(
                                          height: index !=
                                                  widget.product.choiceOptions!
                                                          .length -
                                                      1
                                              ? Dimensions.PADDING_SIZE_LARGE
                                              : 0),
                                    ]);
                              },
                            ),
                            SizedBox(
                                height:
                                    widget.product.choiceOptions?.isNotEmpty ==
                                            true
                                        ? Dimensions.PADDING_SIZE_LARGE
                                        : 0),
                            GetBuilder<CartController>(
                                builder: (cartController) {
                              return Column(
                                children: [
                                  Row(children: [
                                    Text('quantity'.tr, style: sfMedium),
                                    Expanded(child: SizedBox()),
                                    Row(children: [
                                      QuantityButton(
                                        isActive: _isAvailable,
                                        onTap: _isAvailable
                                            ? () {
                                                _cartModel.quantity > 1
                                                    ? productController
                                                        .setQuantity(false)
                                                    : null;
                                                setState(() {});
                                              }
                                            : () {},
                                        isIncrement: false,
                                      ),
                                      Text(
                                          productController.quantity.toString(),
                                          style: sfMedium.copyWith(
                                              fontSize: Dimensions
                                                  .fontSizeExtraLarge)),
                                      QuantityButton(
                                        isActive: _isAvailable,
                                        onTap: () {
                                          if (!_isAvailable) {
                                            return;
                                          }

                                          setState(() {
                                            productController.setQuantity(true);
                                          });
                                        },
                                        isIncrement: true,
                                      ),
                                    ]),
                                  ]),
                                  SizedBox(
                                    height: 2.h,
                                  ),
                                  widget.product.addOns?.isNotEmpty == true
                                      ? Padding(
                                          padding: EdgeInsets.zero,
                                          child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: [
                                                Row(
                                                  children: [
                                                    AnimatedBuilder(
                                                      animation: _animation,
                                                      builder:
                                                          (context, child) {
                                                        return Opacity(
                                                          opacity:
                                                              _animation.value,
                                                          child: Text(
                                                            'customize_order'
                                                                .tr,
                                                            style: sfRegular.copyWith(
                                                                color: Theme.of(
                                                                        context)
                                                                    .primaryColor,
                                                                fontWeight:
                                                                    FontWeight
                                                                        .w500),
                                                          ),
                                                        );
                                                      },
                                                    ),
                                                  ],
                                                ),
                                                Container(
                                                  height: 75,
                                                  child: Column(
                                                    children: [
                                                      Expanded(
                                                        child: ListView.builder(
                                                          scrollDirection:
                                                              Axis.horizontal,
                                                          shrinkWrap: true,
                                                          physics:
                                                              BouncingScrollPhysics(),
                                                          itemCount: widget
                                                              .product
                                                              .addOns
                                                              ?.length,
                                                          itemBuilder:
                                                              (context, index) {
                                                            return InkWell(
                                                              onTap: () {
                                                                if (campaignController
                                                                            .addOnQtyList[
                                                                        index] ==
                                                                    0) {
                                                                  campaignController
                                                                      .addAddon(widget
                                                                          .product
                                                                          .addOns![index]);
                                                                  campaignController
                                                                      .setAddOnQuantity(
                                                                          true,
                                                                          index);
                                                                  campaignController.setAddOnPrice(
                                                                      widget
                                                                          .product
                                                                          .addOns![
                                                                              index]
                                                                          .price!,
                                                                      index,
                                                                      true,
                                                                      false);
                                                                } else if (campaignController
                                                                            .addOnQtyList[
                                                                        index] ==
                                                                    1) {
                                                                  campaignController
                                                                      .removeAddon(
                                                                          widget
                                                                              .product
                                                                              .addOns![index],
                                                                          index);
                                                                  campaignController
                                                                      .setAddOnQuantity(
                                                                          false,
                                                                          index);
                                                                  campaignController
                                                                      .setAddOnPrice(
                                                                          0,
                                                                          index,
                                                                          true,
                                                                          false);
                                                                }
                                                                campaignController
                                                                    .updateUi();
                                                              },
                                                              child: Container(
                                                                width: 70,
                                                                height: 80,
                                                                padding: EdgeInsets
                                                                    .all(Dimensions
                                                                        .PADDING_SIZE_EXTRA_SMALL),
                                                                alignment:
                                                                    Alignment
                                                                        .center,
                                                                margin: EdgeInsets.only(
                                                                    right: Dimensions
                                                                        .PADDING_SIZE_DEFAULT,
                                                                    top: Dimensions
                                                                        .PADDING_SIZE_DEFAULT),
                                                                decoration:
                                                                    BoxDecoration(
                                                                  color: campaignController.addOnQtyList[
                                                                              index] >
                                                                          0
                                                                      ? Theme.of(
                                                                              context)
                                                                          .primaryColor
                                                                      : Theme.of(
                                                                              context)
                                                                          .colorScheme
                                                                          .surface,
                                                                  borderRadius:
                                                                      BorderRadius.circular(
                                                                          Dimensions
                                                                              .RADIUS_SMALL),
                                                                  border: campaignController.addOnQtyList[
                                                                              index] >
                                                                          0
                                                                      ? null
                                                                      : Border.all(
                                                                          color: Theme.of(context)
                                                                              .disabledColor,
                                                                          width:
                                                                              2),
                                                                  boxShadow:
                                                                      campaignController.addOnQtyList[index] >
                                                                              0
                                                                          ? [
                                                                              BoxShadow(color: Colors.grey[Get.isDarkMode ? 700 : 300]!, blurRadius: 5, spreadRadius: 1)
                                                                            ]
                                                                          : null,
                                                                ),
                                                                child: Column(
                                                                    children: [
                                                                      Expanded(
                                                                        child: Column(
                                                                            mainAxisAlignment:
                                                                                MainAxisAlignment.center,
                                                                            children: [
                                                                              Text(
                                                                                widget.product.addOns?[index].name ?? '',
                                                                                maxLines: 2,
                                                                                overflow: TextOverflow.ellipsis,
                                                                                textAlign: TextAlign.center,
                                                                                style: sfMedium.copyWith(
                                                                                  color: campaignController.addOnQtyList[index] > 0 ? Colors.white : Colors.black,
                                                                                  fontSize: Dimensions.fontSizeSmall,
                                                                                ),
                                                                              ),
                                                                              SizedBox(height: 5),
                                                                              Text(
                                                                                widget.product.addOns![index].price! > 0
                                                                                    ? campaignController.addOnQtyList[index] == 0
                                                                                        ? PriceConverter.convertPrice(widget.product.addOns![index].price!)
                                                                                        : PriceConverter.convertPrice(widget.product.addOns![index].price! * campaignController.addOnQtyList[index])
                                                                                    : 'free'.tr,
                                                                                maxLines: 1,
                                                                                overflow: TextOverflow.ellipsis,
                                                                                style: sfRegular.copyWith(
                                                                                  color: campaignController.addOnQtyList[index] > 0 ? Colors.white : Colors.black,
                                                                                  fontSize: Dimensions.fontSizeExtraSmall,
                                                                                ),
                                                                              ),
                                                                            ]),
                                                                      ),
                                                                      campaignController.addOnQtyList[index] >
                                                                              0
                                                                          ? Container(
                                                                              height: 15,
                                                                              decoration: BoxDecoration(borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL), color: Theme.of(context).cardColor),
                                                                              child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                                                                                Expanded(
                                                                                  child: InkWell(
                                                                                    onTap: () {
                                                                                      if (campaignController.addOnQtyList[index] == 1) {
                                                                                        campaignController.removeAddon(widget.product.addOns![index], index);
                                                                                        campaignController.setAddOnQuantity(false, index);
                                                                                        campaignController.setAddOnPrice(0, index, true, false);
                                                                                      } else {
                                                                                        campaignController.setAddOnQuantity(false, index);
                                                                                      }
                                                                                      campaignController.updateUi();
                                                                                    },
                                                                                    child: Center(child: Icon(Icons.remove, size: 15)),
                                                                                  ),
                                                                                ),
                                                                                Text(
                                                                                  campaignController.addOnQtyList[index].toString(),
                                                                                  style: sfMedium.copyWith(fontSize: Dimensions.fontSizeSmall),
                                                                                ),
                                                                                Expanded(
                                                                                  child: InkWell(
                                                                                    onTap: () {
                                                                                      campaignController.setAddOnQuantity(true, index);
                                                                                    },
                                                                                    child: Center(child: Icon(Icons.add, size: 15)),
                                                                                  ),
                                                                                ),
                                                                              ]),
                                                                            )
                                                                          : SizedBox(),
                                                                    ]),
                                                              ),
                                                            );
                                                          },
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                )
                                              ]),
                                        )
                                      : SizedBox(),
                                ],
                              );
                            }),
                            SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                            Row(children: [
                              Text('${'total_amount'.tr}:', style: sfMedium),
                              SizedBox(
                                  width: Dimensions.PADDING_SIZE_EXTRA_SMALL),
                              Text(
                                  PriceConverter.convertPrice(
                                    priceWithAddons,
                                  ),
                                  style: sfBold.copyWith(
                                      color: Theme.of(context).primaryColor)),
                            ]),
                            SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                            _isAvailable
                                ? SizedBox()
                                : Container(
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.all(
                                        Dimensions.PADDING_SIZE_SMALL),
                                    margin: EdgeInsets.only(
                                        bottom: Dimensions.PADDING_SIZE_SMALL),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(
                                          Dimensions.RADIUS_SMALL),
                                      color: Theme.of(context)
                                          .primaryColor
                                          .withOpacity(0.1),
                                    ),
                                    child: _isRestAvailable == false
                                        ? Text(
                                            'restaurant_is_closed_now'.tr,
                                            style: sfMedium.copyWith(
                                              color: Theme.of(context)
                                                  .primaryColor,
                                              fontSize:
                                                  Dimensions.fontSizeLarge,
                                            ),
                                          )
                                        : Column(children: [
                                            Text('not_available_now'.tr,
                                                style: sfMedium.copyWith(
                                                  color: Theme.of(context)
                                                      .primaryColor,
                                                  fontSize:
                                                      Dimensions.fontSizeLarge,
                                                )),
                                            Text(
                                              '${'available_will_be'.tr} ${DateConverter.convertTimeToTime(widget.product.availableTimeStarts!)} '
                                              '- ${DateConverter.convertTimeToTime(widget.product.availableTimeEnds!)}',
                                              style: sfRegular,
                                            ),
                                          ]),
                                  ),
                            (widget.product.scheduleOrder != true &&
                                    !_isAvailable)
                                ? SizedBox()
                                : Row(children: [
                                    SizedBox(
                                        width: Dimensions.PADDING_SIZE_SMALL),
                                    Expanded(
                                        child: CustomButton(
                                      width: ResponsiveHelper.isDesktop(context)
                                          ? MediaQuery.of(context).size.width /
                                              2.0
                                          : null,
                                      buttonText: widget.isCampaign
                                          ? 'order_now'.tr
                                          : widget.cart != null
                                              ? 'update_in_cart'.tr
                                              : 'add_to_cart'.tr,
                                      onPressed: () {
                                        Get.back();

                                        Function() callback = () {
                                          if (widget.isCampaign) {
                                            if (Get.find<CartController>()
                                                .isExistInCart(
                                                    _cartModel, false, 0)) {
                                              Get.find<CartController>()
                                                  .addToCart(_cartModel);
                                            }

                                            Get.toNamed(
                                                RouteHelper.getCheckoutRoute(
                                                    'campaign'),
                                                arguments: CheckoutScreen(
                                                  fromCart: false,
                                                  cartList: [_cartModel],
                                                  restaurantID: _cartModel
                                                      .product?.restaurantId,
                                                ));
                                          } else {
                                            if (Get.find<CartController>()
                                                .existAnotherRestaurantProduct(
                                                    _cartModel.product!
                                                        .restaurantId!)) {
                                              Get.find<CartController>()
                                                  .addToCart(_cartModel);
                                            } else {
                                              Get.find<CartController>()
                                                  .addToCart(_cartModel);
                                            }
                                          }
                                        };
                                        if (Get.find<AuthController>()
                                                .isLoggedIn() &&
                                            widget.product.is_alcohol == true) {
                                          if (Get.find<UserController>()
                                                  .userInfoModel
                                                  ?.birthDate
                                                  ?.isNotEmpty ==
                                              true) {
                                            DateTime birthday = DateTime.parse(
                                                Get.find<UserController>()
                                                    .userInfoModel!
                                                    .birthDate!);
                                            if ((DateTime.now()
                                                        .difference(birthday)
                                                        .inHours /
                                                    YearInHour) >=
                                                21) {
                                              callback();
                                            } else {
                                              Get.dialog(AgeDialog(
                                                callback: callback,
                                              ));
                                            }
                                          } else {
                                            Get.dialog(AgeDialog(
                                              callback: callback,
                                            ));
                                          }
                                        } else {
                                          callback();
                                        }
                                      },
                                    )),
                                  ]),
                          ]),
                    ),
                  ]),
            ),
          );
        });
      }),
    );
  }

  void _showCartSnackBar(BuildContext context) {
    Get.showSnackbar(GetSnackBar(
      backgroundColor: Colors.green,
      message: 'item_added_to_cart'.tr,
      mainButton: TextButton(
        onPressed: () => Get.toNamed(RouteHelper.getCartRoute()),
        child: Text('view_cart'.tr,
            style: sfMedium.copyWith(color: Theme.of(context).cardColor)),
      ),
      onTap: (_) => Get.toNamed(RouteHelper.getCartRoute()),
      duration: Duration(seconds: 3),
      maxWidth: Dimensions.WEB_MAX_WIDTH,
      snackStyle: SnackStyle.FLOATING,
      margin: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
      borderRadius: 10,
      isDismissible: true,
      dismissDirection: DismissDirection.horizontal,
    ));
  }
}
